<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\learn;

use app\common\model\learn\ArticleCategory as ArticleCategoryModel;
use think\facade\Db;

/**
 *
 * Class Article
 * @package app\api\model
 */
class ArticleCategory extends ArticleCategoryModel {

	public function setDefault($userId) {

		Db::name('user')->where('user_id', $userId)->update(['article_id' => $this->category_id]);
		$book = Db::name('learn_article_category_user')
			->where('user_id', $userId)
			->where('book_id', $this->category_id)->where('is_delete', 0)->find();
		if (!$book) {
			$insertData['book_id'] = $this->category_id;
			$insertData['user_id'] = $userId;
			$insertData['num'] = 5; //默认
			$insertData['total_num'] = Db::name('learn_article_title')->where('book_name', $this['name'])->count();
			$insertData['store_id'] = self::$storeId;
			$insertData['create_time'] = time();
			Db::name('learn_article_category_user')->insert($insertData);
		} else {
			$insertData['total_num'] = Db::name('learn_article_title')->where('book_name', $this['name'])->count();
			Db::name('learn_article_category_user')
				->where('user_id', $userId)
				->where('book_id', $this->category_id)
				->update($insertData);
		}
		return true;
	}
}